package cn.com.victorysoft.ylgl.service.commons.web;

import javax.servlet.http.HttpServletRequest;

/***
 * @author gechaoqing
 */
public class RequestContext {
    private static final ThreadLocal<RequestContext> REQUEST_CONTEXT_HOLDER = new ThreadLocal<>();
    private HttpServletRequest request;
    private User user;


    public static void init(HttpServletRequest request) {
        RequestContext requestContext = new RequestContext(request);
        REQUEST_CONTEXT_HOLDER.set(requestContext);
    }

    public static void destroy() {
        REQUEST_CONTEXT_HOLDER.remove();
    }

    public static String getCurrentUserName() {
        return (REQUEST_CONTEXT_HOLDER.get()).request.getRemoteUser();
    }

    public static User getCurrentUser() {
        return (REQUEST_CONTEXT_HOLDER.get()).user;
    }

    private RequestContext(HttpServletRequest request) {
        this.request = request;
        if (null == this.user) {
            this.user = new User("Guest", "123456");
            this.user.setId("000000");
        }
    }

}
